import os
from PyQt5.QtWidgets import *
from .done import *

class DoneUI(Ui_Done):
    def __init__(self):
        self.returnHandle = ''
        self.againHandle = ''
        self.printHandle = ''
        self.allHandle = ''

    def init(self):
        # TODO 优化 2 设置水平方向表格为自适应的伸缩模式
        self.cardsTW.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)

        # TODO 优化3 将表格变为禁止编辑
        self.cardsTW.setEditTriggers(QAbstractItemView.NoEditTriggers)

        # TODO 优化 4 设置表格整行选中
        self.cardsTW.setSelectionBehavior(QAbstractItemView.SelectRows)

        # TODO 优化 5 将行与列的高度设置为所显示的内容的宽度高度匹配
        QTableWidget.resizeColumnsToContents(self.cardsTW)
        QTableWidget.resizeRowsToContents(self.cardsTW)

        # TODO 优化 6 表格头的显示与隐藏
        # self.cardsTW.verticalHeader().setVisible(False)
        # self.cardsTW.horizontalHeader().setVisible(False)

    def load(self, id):
        pass

    def getUnprintBarcodes(self, id):
        if(os.path.exists(f"./config/flowCard/{id}.config")):
            with open(f"./config/flowCard/{id}.config", "r") as fp:
                data = eval(fp.read())
                if(data['total']>data['count']):
                    return data['barcodes'][data['count']]
        else:
            return

    def printed(self, id):
        if (os.path.exists(f"./config/flowCard/{id}.config")):
            with open(f"./config/flowCard/{id}.config", "r") as fp:
                data = eval(fp.read())
                if (data['total'] > data['count']):
                    data['count'] += 1
                    with open(f"./config/flowCard/{id}.config", "w+") as f:
                        f.write(str(data).replace("'", '"'))
                        return True
        else:
            return

    def returnView(self):
        self.returnHandle()

    def again(self):
        self.againHandle()

    def printCode(self):
        self.printHandle()

    def all(self):
        self.allHandle()